
//渲染管理员界面
let searchAdmin=document.querySelector('#searchAdmin')
let addAdmin=document.querySelector('#addAdmin')
let oTb6=document.querySelector('.tbody6')
let pagination5=document.querySelector('.page5')

function toAdmin(){
    $.ajax({
        url:'/toAdmin',
        data:{
            search:searchAdmin.value,
            page:page,
            count:count
        },
        method:'post',
        success:(res)=>{


                    oTb6.innerHTML='';
                    pagination5.innerHTML=`
                    <li>
                        <a class="left" aria-label="Previous">
                        <span class="left" aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li>
                        <a class="right" aria-label="Next">
                        <span class="right" aria-hidden="true">&raquo;</span>
                        </a>
                    </li>`
                    res.data.forEach((v)=> {
                        let oTr=document.createElement('tr')
                        oTr.innerHTML=`
                        <td> ${v.a_id}</td>
                        <td> ${v.a_name}</td>
                        <td> ${v.a_user}</td>
                        <td> ${v.a_pass}</td>
                        <td> ${v.a_status==0?'超级管理员':'普通管理员'}</td>
                        <td>
                        <a class="delAdmin">删除</a>
                        <a class="editAdmin" data-toggle="modal" data-target="#editAdmin"> 修改</a>
                        </td>
                        `
                        oTb6.appendChild(oTr)
                    });
                    //渲染页码
                    let pageCount=Math.ceil(res.total.total/count)
                    for(let i=1;i<=pageCount;i++){
                        let oLi=document.createElement('li')
                        oLi.innerHTML=`<a class="page">${i}</a>`
                        if(i==page){
                            oLi.classList.add('active')
                        }
                        pagination5.insertBefore(oLi,pagination5.lastElementChild)
                    }
        }
    })
}
toAdmin()

//切换页码
chanFn=()=>{
    if(event.target.className=='page'){
        page=Number(event.target.innerText)
        toAdmin()
    }
}
pagination5.addEventListener('click',chanFn)
//上一页下一页 
toLeft=()=>{
    if(event.target.className=='left'){
        if(page>1){
            page--
            toAdmin()
        }
    }
}
pagination5.addEventListener('click',toLeft)

toRight=()=>{
    if(event.target.className=='right'){
        if(page<pagination5.children.length-2){
            page++

            toAdmin()
        }
        
    }
}
pagination5.addEventListener('click',toRight)


//新增管理员
let adminName=document.querySelector('#adminName')
let adminUser=document.querySelector('#adminUser')
let adminPass=document.querySelector('#adminPass')
let editSymbol=document.querySelector('#editSymbol')


let addAdminSave=document.querySelector('.addAdminSave')
addAdminSave.onclick=()=>{
    $.ajax({
        url:'/toAddAdmin',
        data:{
            adminName:adminName.value,
            adminUser:adminUser.value,
            adminPass:adminPass.value,
            editSymbol:editSymbol.value
        },
        method:'post',
        success:(res)=>{
            if(res){
                alert('添加成功！')
                adminName.value=''
                adminUser.value=''
                adminPass.value=''
                editSymbol.value=''
            }
        }
    })
    toAdmin()
}

//删除
DelAdmin=()=>{
    if(event.target.className=='delAdmin'){
        $.ajax({
            url:'/AdminDel',
            data:{
                ID:event.target.parentNode.parentNode.firstElementChild.innerText
            },
            method:'post',
            success:(res)=>{
                if(res=='删除成功'){
                    alert('删除成功！')
                    toAdmin()
                    
                }
            }

        })
    }
}
oTb6.addEventListener('click',DelAdmin)
//修改
let editAdminSave=document.querySelector('.editAdminSave')
let adminType1=document.querySelector('#editSymbol1')
let adminId1=document.querySelector('#adminId1')
let adminUser1=document.querySelector('#adminUser1')
let adminPass1=document.querySelector('#adminPass1')
let adminName1=document.querySelector('#adminName1')

editAdmin=()=>{
    if(event.target.className=='editAdmin'){
        console.log(1)
        adminId1.value=event.target.parentNode.parentNode.children[0].innerText
        adminName1.value=event.target.parentNode.parentNode.children[1].innerText
        adminUser1.value=event.target.parentNode.parentNode.children[2].innerText
        adminPass1.value=event.target.parentNode.parentNode.children[3].innerText
        adminType1.value=event.target.parentNode.parentNode.children[4].innerText
    } 
}
oTb6.addEventListener('click',editAdmin)
editAdminSave.onclick=()=>{
    $.ajax({
        url:'/editAdmin',
        data:{
            adminId1:adminId1.value,
            adminName1:adminName1.value,
            adminUser1:adminUser1.value,
            adminPass1:adminPass1.value,
            adminType1:adminType1.value
        },
        method:'post',
        success:(res)=>{
            if(res=='修改成功'){
                alert('修改成功！')
                toAdmin()
            }else{
                console.log(res)
            }
        }
    })
}
//搜索
let searchAdminBtn=document.querySelector('#searchAdminBtn')

searchAdminBtn.onclick=()=>{
    toAdmin()
}
//重置
let resetAdmin=document.querySelector('#resetAdmin')
resetAdmin.onclick=()=>{
    searchAdmin.value=''
    toAdmin()
}